package com.atguigu.msnservice.controller;

import com.atguigu.commonutils.RandomUtil;
import com.atguigu.commonutils.Response;
import com.atguigu.msnservice.service.MsmService;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author zhaolie
 * @Date 2020/12/17 4:38 下午
 * @Version 1.0
 * @Description
 */

@RestController
@RequestMapping("/edumsm")
@Api(description = "阿里云短信功能")
public class MsmController {

    @Resource
    private MsmService msmService;

    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("/{phone}")
    @ApiOperation("阿里云短信发送")
    private Response sendMsm(@PathVariable("phone") String phone) {
        Object rdcode = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(rdcode)) {
            return Response.SUCCESS();
        }
        String code = RandomUtil.getSixBitRandom();
        Map<String, Object> param = new HashMap<>();
        param.put("code", code);
        Boolean isSend = msmService.send(param, phone);
        if (isSend) {
            redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
            return Response.SUCCESS();
        } else {
            return Response.Failed().message("短信发送失败");
        }

    }
}
